 package com.timber.panels;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import pv.jfcx.JPVCurrency;
import pv.jfcx.JPVDate;
import pv.jfcx.JPVDatePlus;

import com.borland.jbcl.layout.VerticalFlowLayout;
import com.timber.frame.ImsFrame;
import com.timber.tableModels.InventoryTableModel;
import com.timber.util.Global;
import com.timber.util.ImageMgr;
import com.timber.util.ImsFrameComponent;
import com.timber.util.InventoryMgr;
import com.timber.util.SLDataChangeEvent;
import com.timber.util.SLTableView;
import com.timber.util.SearchableComboBox;
import com.timber.util.TimberClassMgr;
import com.timber.util.TimberNameMgr;

public class InventoryPanel   extends ImsFrameComponent{
	  protected TitledBorder		titledBorder1;
	  protected TitledBorder		bottomBorder1;
	  protected JPanel			topPanel = new JPanel();
	  protected JPanel			SerachPanel = new JPanel();
	  protected JPanel			bottomPanel = new JPanel();
	  protected InventoryTableModel	lnDetailModel = new InventoryTableModel();
	  protected SLTableView			inDetailTV = new SLTableView( lnDetailModel );
	  protected SearchableComboBox		timberNameCB;
	  protected SearchableComboBox		timberClassCB;
	  protected TimberNameMgr objTimberNameMgr =new TimberNameMgr();
	  private static final Dimension cbDim = new Dimension( 200, 60 );
	 // private JComboBox	timberNameCB;
	  //private JComboBox	timberClassCB;
	  private JComboBox	timberLength;
	  protected JPVCurrency LengthTF = new JPVCurrency();
	  protected JLabel LengthLabel; 
	  protected JPVCurrency widthTF = new JPVCurrency();
	  protected JLabel 		widthLabel;

	  protected JLabel		fromRecDateLabel = new JLabel();
	  protected JLabel		toRecDateLabel = new JLabel();
	  protected JPVDatePlus		fromRecDateDP = new JPVDatePlus();
	  protected JPVDatePlus		toRecDateDP = new JPVDatePlus();
	  
	  protected JPanel		        recDatePanel = new JPanel();
	  
	  protected TitledBorder	 titledBorder3;
	  protected GridBagLayout		gridBagLayout4 = new GridBagLayout();
	  protected JPanel			queryPanel = new JPanel();
	  protected GridBagLayout		gridBagLayout2 = new GridBagLayout();
	  protected JLabel			SupplierLabel = new JLabel( "Supplier" );
	  protected JComboBox			SupplierCB;
	  
	  protected JPVCurrency rateTF = new JPVCurrency();
	  protected JLabel rateLabel; 
	  protected ImageIcon			goImage;
	  protected JButton			runBtn = new JButton();
	  protected JPanel			buttonPanel = new JPanel();
	  protected VerticalFlowLayout		verticalFlowLayout1 = new VerticalFlowLayout();

	  public InventoryPanel(ImsFrame parent) {
	    super( parent );
	    try
	    {
	      jbInit();
	    }
	    catch( Exception ex )
	    {
	      ex.printStackTrace();
	    }
	  }



	public void onDataChange(SLDataChangeEvent e) {
		
		
	}
	
	

	
	  void jbInit() throws Exception
	  {
	    //this.setLayout( new GridBagLayout() );
		  goImage       = ImageMgr.getIcon( ImageMgr.GO32_ICON );
		   buttonPanel.setLayout( verticalFlowLayout1 );

		titledBorder1 = new TitledBorder( BorderFactory.createEtchedBorder(), "Selection Criteria" );
	    bottomBorder1=new TitledBorder( BorderFactory.createEtchedBorder(), "Invetory Details" );
	    titledBorder3 = new TitledBorder( BorderFactory.createEtchedBorder(), " Date" );
	    topPanel.setBorder( titledBorder1 );
	    topPanel.setLayout( new GridBagLayout() );
	    
	    topPanel.setMinimumSize(new Dimension(840, 250));
	    topPanel.setPreferredSize(new Dimension(840, 250));
//	    
//	    bottomPanel.setBorder( bottomBorder1 );
//	    bottomPanel.setLayout( new GridBagLayout() );
	    
	    bottomPanel.setMinimumSize(new Dimension(900, 500));
	    bottomPanel.setPreferredSize(new Dimension(900, 500));

	    bottomPanel.setBorder( new TitledBorder( BorderFactory.createEtchedBorder(), " Result" ) );
	    bottomPanel.setLayout(gridBagLayout4 );
	    
	    //queryPanel.setBorder( titledBorder1 );
	    queryPanel.setLayout( gridBagLayout2 );
	    
	    
	    
	    timberNameCB = new SearchableComboBox( Global.getMainFrame(),
	    		TimberNameMgr.mgr.getDataVector(),
                SearchableComboBox.INCLUDE_ALL,
                true );
	    
	   // timberNameCB = new   JComboBox(TimberNameMgr.mgr.getDataVector() );
	    timberNameCB.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),"Name"));
	    timberNameCB.setEditable( true );
	    timberNameCB.setSize(new Dimension(120,40));
	    //timberNameCB.setPreferredSize(new Dimension(120,40));
	   // timberNameCB.setPreferredSize( cbDim );
//	    timberNameCB.addItemListener( new ItemListener()
//	      {
//	        public void itemStateChanged( ItemEvent ie )
//	        {
//	        	System.out.println("Changed the item");
//	        	 
//	          String country = ( String )timberNameCB.getSelectedItem();
//	          System.out.println(country);
//	           if( country.equals( SLDataMgr.SELECT_ALL ) )
//	          {
//	        	  timberNameCB.setSelectedIndex( 0 );
//	          }
//	           
//	        }
//	      } );
//	    
	    SupplierCB=new JComboBox(TimberNameMgr.mgr.getDataVector()  );
	    SupplierCB.setPreferredSize( new Dimension( 140, 26 ) );
	    timberClassCB =  new SearchableComboBox( Global.getMainFrame(),
	    		TimberClassMgr.mgr.getDataVector(),
                SearchableComboBox.INCLUDE_ALL,
                true );
	    timberClassCB.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),"Class"));
	    timberClassCB.setEditable( true );
	    timberClassCB.setSize(50,40);
	    
	    LengthLabel = new JLabel("Length");
	    widthLabel = new JLabel("Width");
	    
		Dimension d = new Dimension(80,20);
	    LengthTF.setDecimals(4);
	    LengthTF.setSymbol("");
	    LengthTF.setText("");        
	    LengthTF.setPreferredSize(d);
	    LengthTF.setMinimumSize(d);
	    LengthTF.setAlignment(2);
	    
	   widthTF.setDecimals(4);
	   widthTF.setSymbol("");
	   widthTF.setText("");        
	   widthTF.setPreferredSize(d);
	   widthTF.setMinimumSize(d);
	   widthTF.setAlignment(2);
	   
	   rateLabel= new JLabel("Rate");
	   rateTF.setDecimals(4);
	   rateTF.setSymbol("");
	   rateTF.setText("");        
	   rateTF.setPreferredSize(d);
	   rateTF.setMinimumSize(d);
	   rateTF.setAlignment(2);
	   
	   
	   
	   // run button
	   
	    runBtn.setIcon( goImage );
	    runBtn.setMargin( insetsNone );
	    runBtn.addActionListener( new ActionListener()
	      {
	        public void actionPerformed( ActionEvent e )
	        {
	        	  //parent.getGlassPane().setVisible( true );
	          getDetail( );
	          //parent.getGlassPane().setVisible( false );
	        }

			
	      } );
	   
	 // adding Date 
	   
	   

	    fromRecDateLabel.setHorizontalAlignment( SwingConstants.LEFT );
	    fromRecDateLabel.setText( "From" );
	    toRecDateLabel.setHorizontalAlignment( SwingConstants.LEFT );
	    toRecDateLabel.setText( "To" );

	    fromRecDateDP.setDate( null );
	    fromRecDateDP.setFormat( JPVDate.DMY );
	    fromRecDateDP.setMonthFormat( JPVDate.MMM );
	    fromRecDateDP.setSeparator( "-" );
	    fromRecDateDP.setCloseOnDoubleClick( false );
	    fromRecDateDP.addFocusListener( new FocusAdapter()
	      {
	        public void focusGained( FocusEvent fe )
	        {
	          fromRecDateDP.setCaretPosition( 0 );
	        }
	      });

	    toRecDateDP.setDate( null );
	    toRecDateDP.setFormat( JPVDate.DMY );
	    toRecDateDP.setMonthFormat( JPVDate.MMM );
	    toRecDateDP.setSeparator( "-" );
	    toRecDateDP.setCloseOnDoubleClick( false );
	    toRecDateDP.addFocusListener( new FocusAdapter()
	      {
	        public void focusGained( FocusEvent fe )
	        {
	          toRecDateDP.setCaretPosition( 0 );
	        }
	      });
	   

	    recDatePanel.setBorder( titledBorder3 );
	    recDatePanel.setLayout(gridBagLayout4 );
	   
	   // adding to top panel
	   
	   // topPanel.add(timberNameCB);
	    //topPanel.add(timberClassCB);
	  //  topPanel.add(LengthLabel);
	  // topPanel.add(LengthTF);
	  //  topPanel.add(widthLabel);
	    //topPanel.add(widthTF);
	   // topPanel.add(fromRecDateDP);
	    //topPanel.add(toRecDateDP);
	    
	    
	    
	    
	    // projected record date panel setup

	    recDatePanel.add( fromRecDateLabel,
	                      new GridBagConstraints( 0,
	                                              0,
	                                              1,
	                                              1,
	                                              0.0,
	                                              0.0,
	                                              GridBagConstraints.NORTHWEST,
	                                              GridBagConstraints.NONE,
	                                              new Insets( 0, 0, 0, 0 ), 0, 0 ) );
	    recDatePanel.add( toRecDateLabel,
	                      new GridBagConstraints( 3,
	                                              0,
	                                              1,
	                                              1,
	                                              0.0,
	                                              0.0,
	                                              GridBagConstraints.NORTHWEST,
	                                              GridBagConstraints.NONE,
	                                              new Insets( 0, 5, 0, 0 ), 0, 0 ) );
	    recDatePanel.add( fromRecDateDP,
	                      new GridBagConstraints( 0,
	                                              1,
	                                              3,
	                                              1,
	                                              0.0,
	                                              0.0,
	                                              GridBagConstraints.NORTHWEST,
	                                              GridBagConstraints.NONE,
	                                              new Insets( 0, 0, 0, 0 ), 0, 0 ) );
	    recDatePanel.add( toRecDateDP,
	                      new GridBagConstraints( 3,
	                                              1,
	                                              3,
	                                              1,
	                                              0.0,
	                                              0.0,
	                                              GridBagConstraints.NORTHWEST,
	                                              GridBagConstraints.NONE,
	                                              new Insets( 0, 5, 0, 0 ), 0, 0 ) );
	
	    
	    // projected record date panel end
	    
	    
	    
	   // adding Query panel start 
	    
	    
	    
	    queryPanel.add( timberNameCB,
                new GridBagConstraints( 0,
                                        0,
                                        1,
                                        1,
                                        0.0,
                                        0.0,
                                        GridBagConstraints.NORTHWEST,
                                        GridBagConstraints.NONE,
                                        insetsNone, 0, 0 ) );
	queryPanel.add( timberClassCB,
                new GridBagConstraints( 1,
                                        0,
                                        1,
                                        1,
                                        0.0,
                                        0.0,
                                        GridBagConstraints.NORTHWEST,
                                        GridBagConstraints.NONE,
                                        insetsFiveL, 0, 0 ) );
	queryPanel.add( LengthLabel,
                new GridBagConstraints( 2,
                                        0,
                                        1,
                                        1,
                                        0.0,
                                        0.0,
                                        GridBagConstraints.NORTHWEST,
                                        GridBagConstraints.NONE,
                                        insetsFiveL, 0, 0 ) );
	queryPanel.add( LengthTF,
                new GridBagConstraints( 3,
                                        0,
                                        1,
                                        1,
                                        0.0,
                                        0.0,
                                        GridBagConstraints.NORTHWEST,
                                        GridBagConstraints.NONE,
                                        insetsFiveL, 0, 0 ) );
	
	queryPanel.add( widthLabel,
            new GridBagConstraints( 4,
                                    0,
                                    1,
                                    1,
                                    0.0,
                                    0.0,
                                    GridBagConstraints.NORTHWEST,
                                    GridBagConstraints.NONE,
                                    insetsFiveL, 0, 0 ) );
queryPanel.add( widthTF,
            new GridBagConstraints( 5,
                                    0,
                                    1,
                                    1,
                                    0.0,
                                    0.0,
                                    GridBagConstraints.NORTHWEST,
                                    GridBagConstraints.NONE,
                                    insetsFiveL, 0, 0 ) );
    
	    
queryPanel.add( recDatePanel,
        new GridBagConstraints( 0,
                                1,
                                1,
                                1,
                                0.0,
                                0.0,
                                GridBagConstraints.NORTHWEST,
                                GridBagConstraints.NONE,
                                insetsNone, 0, 0 ) );




queryPanel.add( SupplierLabel,
        new GridBagConstraints( 1,
                                1,
                                1,
                                1,
                                0.0,
                                0.0,
                                GridBagConstraints.NORTHWEST,
                                GridBagConstraints.NONE,
                                insetsNone, 0, 0 ) );
queryPanel.add( SupplierCB,
        new GridBagConstraints( 1,
                                2,
                                1,
                                1,
                                0.0,
                                0.0,
                                GridBagConstraints.NORTHWEST,
                                GridBagConstraints.NONE,
                                insetsNone, 0, 0 ) );



queryPanel.add( rateLabel,
        new GridBagConstraints( 1,
                                3,
                                1,
                                1,
                                0.0,
                                0.0,
                                GridBagConstraints.NORTHWEST,
                                GridBagConstraints.NONE,
                                insetsNone, 0, 0 ) );
queryPanel.add( rateTF,
        new GridBagConstraints( 1,
                                4,
                                1,
                                1,
                                0.0,
                                0.0,
                                GridBagConstraints.NORTHWEST,
                                GridBagConstraints.NONE,
                                insetsNone, 0, 0 ) );


	    
	    // end 
	
topPanel.add(queryPanel);  
	    
	    // adding Query panel start 
	    
//	    this.add(timberNameCB);
//	    this.add(timberClassCB);
	    
	    
	    

topPanel.add( buttonPanel, null );
buttonPanel.add( runBtn, null );	 
	    
	    
//	    
	    this.add( topPanel,
		      new GridBagConstraints( 0,
					      0,
					      GridBagConstraints.REMAINDER,
					      1,
					      100.0, 0.0,
					      GridBagConstraints.NORTHWEST,
					      GridBagConstraints.NONE,
					      new Insets( 0, 0, 0, 0 ), 0, 0 ) );
	    
	    
	        
	    
	  //  lnDetailTblDoSelect(0,0);
	    addinDetailTV();
	  }
//	  
	
	  
	  
	  
	  protected void addinDetailTV()
	  {
		  System.out.println("added addinDetailTV()");
		  inDetailTV.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
		  inDetailTV.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_NEVER );
		  inDetailTV.setEditable( false );
		  inDetailTV.setShowSortIcons( false );
		  inDetailTV.setEnableSortData( false );
		  inDetailTV.getTable().setCellSelectionEnabled( false );
		  inDetailTV.getTable().setColumnSelectionAllowed( false );
		  inDetailTV.getTable().setRowSelectionAllowed( false );
		  inDetailTV.setVisibleRowCount( 20 );

		  bottomPanel.add( inDetailTV,
			  new GridBagConstraints( 0, //GridBagConstraints.RELATIVE,
						  0,
						  GridBagConstraints.REMAINDER,
						  1,
						  100.0, 100.0,
						  GridBagConstraints.NORTH,
						  GridBagConstraints.HORIZONTAL,
						  new Insets( 0, 0, 0, 0 ), 0, 0 ) );
		  // adding Query result table 
		  
		  this.add( bottomPanel,
			      new GridBagConstraints( 0,
						      0,
						      GridBagConstraints.REMAINDER,
						      1,
						      100.0, 0.0,
						      GridBagConstraints.NORTHWEST,
						      GridBagConstraints.NONE,
						      new Insets( 0, 0, 0, 0 ), 0, 0 ) );
		  

		 // topPanel.validate();
		  
	  }
	  
	  // for getting details 
	  private void getDetail() {
		  Vector v=new Vector() ;
		  //parent.getGlassPane().setVisible( true );
		  System.out.println("Inside  getDetail() ");
		  InventoryMgr obj =new InventoryMgr();
		  v=obj.getInventoryDetails();
		  System.out.println("from panel -->"+v.size());

		  lnDetailModel.setTableData(v);
		
		  lnDetailModel.fireTableDataChanged();
		 
			
		}
	  
	  protected void lnDetailTblDoSelect( int row, int col )
	  {
	    Vector invCols;
	    activateTab( ImsFrame.INVDETAIL_PANEL, new Object() );
	  }
	  
	  
}


